/**
 * @author Administrator
 */
var undefined;
var $ = window.$ = function(xDom){
    return new SimJS.dom(xDom);
};
var SimJS = {
    version: '1.0.0.20090711',
    each: function(obj, callback, args){
        var len = obj.length, key, i = 0;
        if (len === undefined) {
            for (key in obj) {
                if (callback.call(obj[key], k, obj[key], args) === false) 
                    break;
            }
        }
        else {
            for (; i < len && callback.call(obj[i], i, obj[i], args) !== false; i++) {
            }
        }
        return obj;
    }
};
SimJS.dom = function(xDom){
    xDom = xDom || document;
    //DomElement
    if (xDom.nodeType) {
        this[0] = xDom;
        this.length = 1;
        return this;
    }
	
    //ID string
    var regID = /^#([\w-]+)$/.exec(xDom);
    if (regID) {
        this[0] = document.getElementById(regID[1]);
        this.length = 1;
        return this;
    }
	
	//Tag string
	var regTag = xDom.match(/^[a-zA-Z]+[\w-]*$/);
	if (regTag) {
		var tags = document.getElementsByTagName(regTag[0]);
		var arr = [];
		SimJS.each(tags, function(k, v) {
			arr.push(v);
		});
		return this.setArray(arr);
	}
};
SimJS.dom.prototype = {
    html: function(sValue){
        if (sValue == undefined) {
            return this[0].innerHTML;
        }
        else {
            SimJS.each(this, function(p, v){
                v.innerHTML = sValue;
            });
            return this;
        }
    },
    text: function(sText){
        if (sText == undefined) {
            return this[0].innerText || this[0].textContent;
        }
        else {
            if (this[0].innerText) {
                SimJS.each(this, function(p, v){
					v.innerText = sText;
				});
            }
            else {
                SimJS.each(this, function(p, v){
					v.textContent = sText;
				});
            }
        }
    },
	setArray: function(arr) {
		this.length = 0;
		Array.prototype.push.apply(this, arr);
		return this;
	},
	each: function(callback, args) {
		return SimJS.each(this, callback, args);
	}
}
